package fr.efrei.international.java.assignment.two;

import java.awt.event.*;

import javax.swing.*;
import javax.swing.border.BevelBorder;

/**
 * 
 * @author YE JINXIN
 * 
 */
public class IMenuBar extends JMenuBar {

	Record record = null;
	UserData usr = null;
	UsrRegister ur = null;
	MainFrame mf = null;
	WelcomePage wp = null;
	String[] startItems = new String[] { "New user", "Exit" };
	String[] helpItems = new String[] { "Help", "About us" };
	char[] startShortcuts = { 'N', 'E' };
	char[] helpShortcuts = { 'H', 'A' };

	public IMenuBar(UsrRegister ur) {
		this.ur = ur;
		initForWelcomePage();
	}

	public IMenuBar(UserData usr, MainFrame mf,Record record) {
		this.mf = mf;
		this.usr = usr;
		this.record = record;
		initForMainFrame();

	}

	// initialize the menubar for welcomepage frame
	private void initForWelcomePage() {
		JMenu startMenu = new JMenu("Start");
		JMenu helpMenu = new JMenu("Help");

		add(startMenu);
		add(helpMenu);

		JMenuItem newUsertItem = new JMenuItem(startItems[0], startShortcuts[0]);
		JMenuItem exitItem = new JMenuItem(startItems[1], startShortcuts[1]);
		JMenuItem helpItem = new JMenuItem(helpItems[0], helpShortcuts[0]);
		JMenuItem aboutItem = new JMenuItem(helpItems[1], helpShortcuts[1]);
		startMenu.add(newUsertItem);
		startMenu.add(exitItem);
		helpMenu.add(helpItem);
		helpMenu.add(aboutItem);
		setBorder(new BevelBorder(BevelBorder.RAISED));
		aboutItem.addActionListener(new AboutItemListener());
		helpItem.addActionListener(new HelpItemListener());
		exitItem.addActionListener(new ExitItem());
		newUsertItem.addActionListener(new newuserItemListener());
	}

	// initialize the menubar for mainframe
	private void initForMainFrame() {

		
		JMenu review = new JMenu("review");
		JMenu browse = new JMenu("browse");
		JMenu subMenuLevel = new JMenu("level Select");

		JMenuItem browseAllItem = new JMenuItem("borwse all", 'b');
		JMenuItem browseYourRecordsItem = new JMenuItem("browse your records",
				'y');
		
		browseYourRecordsItem.addActionListener(new browseRecordListener());
		// JMenuItem levelSelectItem = new JMenuItem("level Select", 'l');
		JMenuItem reviewExitItem = new JMenuItem("return", 'r');
		JMenuItem level1 = new JRadioButtonMenuItem("Level 1");
		JMenuItem level2 = new JRadioButtonMenuItem("Level 2");
		JMenuItem level3 = new JRadioButtonMenuItem("Level 3");
		JMenuItem level4 = new JRadioButtonMenuItem("Level 4");
		JMenuItem level5 = new JRadioButtonMenuItem("Level 5");
		JMenuItem level6 = new JRadioButtonMenuItem("Level 6");

		level1.addActionListener(new level1Listener());
		level2.addActionListener(new level2Listener());
		level3.addActionListener(new level3Listener());
		level4.addActionListener(new level4Listener());
		level5.addActionListener(new level5Listener());
		level6.addActionListener(new level6Listener());

		// add button group so the every JRadioButtonMenuItem can affect others'
		// behavior
		ButtonGroup group = new ButtonGroup();
		group.add(level1);
		group.add(level2);
		group.add(level3);
		group.add(level4);
		group.add(level5);
		group.add(level6);

		subMenuLevel.add(level1);
		subMenuLevel.add(level2);
		subMenuLevel.add(level3);
		subMenuLevel.add(level4);
		subMenuLevel.add(level5);
		subMenuLevel.add(level6);

		review.add(subMenuLevel);
		review.add(reviewExitItem);
		browse.add(browseAllItem);
		browse.add(browseYourRecordsItem);

		// levelSelectItem.addActionListener(new levelSelectItemListener());
		reviewExitItem.addActionListener(new reviewExitListener());
		browseAllItem.addActionListener(new browseButtonListener());

		add(review);
		add(browse);

	}

	public class newuserItemListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			// usr.test();
			RegisterFrame rf = new RegisterFrame(ur);
		}

	}

	public class level1Listener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			usr.setLevel(1);
			mf.labelForInfor.setText("Current Level: " + usr.getLevel());
			mf.jpForInfor.updateUI();
		}

	}

	public class level2Listener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			usr.setLevel(2);
			mf.labelForInfor.setText("Current Level: " + usr.getLevel());
			mf.jpForInfor.updateUI();
		}

	}

	public class level3Listener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			usr.setLevel(3);
			mf.labelForInfor.setText("Current Level: " + usr.getLevel());
			mf.jpForInfor.updateUI();

		}

	}

	public class level4Listener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			usr.setLevel(4);
			mf.labelForInfor.setText("Current Level: " + usr.getLevel());
			mf.jpForInfor.updateUI();
		}

	}

	public class level5Listener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			usr.setLevel(5);
			mf.labelForInfor.setText("Current Level: " + usr.getLevel());
			mf.jpForInfor.updateUI();
		}

	}

	public class level6Listener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			usr.setLevel(6);
			mf.labelForInfor.setText("Current Level: " + usr.getLevel());
			mf.jpForInfor.updateUI();
		}

	}

	public class browseButtonListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			new BrowseTable();
		}

	}
	public class browseRecordListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			new BrowseTable(record);
		}

	}
	
	public class AboutItemListener implements ActionListener {

		public void actionPerformed(ActionEvent e) {
			AboutUsFrame AF = new AboutUsFrame();

		}
	}

	public class HelpItemListener implements ActionListener {

		public void actionPerformed(ActionEvent e) {
			try {
				HelpFrame HelpF = new HelpFrame();
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		}
	}

	public class ExitItem implements ActionListener {

		public void actionPerformed(ActionEvent e) {
			System.exit(0);
		}
	}

	public class levelSelectItemListener implements ActionListener {

		public void actionPerformed(ActionEvent e) {
			JOptionPane.showMessageDialog(mf, "Bonjour");

		}
	}

	public class reviewExitListener implements ActionListener {

		public void actionPerformed(ActionEvent e) {
			mf.dispose();
			new WelcomePage();

		}
	}

}
